<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>ERP管理系统</title>
  <th:block th:include="include::header"/>
</head>
<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a><cite>首页</cite></a>
                <a><cite>用户管理</cite></a>
                <a>
                    <cite>用户展示</cite></a>
            </span>
  <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
     onclick="location.reload()" title="刷新">
    <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
  </a>
</div>
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body ">
          <form class="layui-form layui-col-space5">
            <div class="layui-form-item">
              <div class="layui-inline">
                <div class="layui-input-inline">
                  <select type="" id="userLockout" name="userfield" required="" lay-verify="required"
                          autocomplete="off" class="layui-input" ><br>
                    <option value="user_id" >用户编号</option>
                    <option value="username" >用户名称</option>
                    <option value="role_name" >用户角色</option>
                  </select>
                </div>
              </div>
              <div class="layui-inline">
                <div class="layui-input-inline">
                  <input type="text" name="condition" placeholder="请输入数据" autocomplete="off"
                         class="layui-input" >
                </div>
                <div class="layui-inline layui-show-xs-block">
                  <button class="layui-btn" lay-submit="" lay-filter="search">
                    <i class="layui-icon">&#xe615;</i>
                  </button>
                </div>
              </div>
            </div>

          </form>
        </div>
        <div class="ayui-card-header">
          <button class="layui-btn layui-btn-danger" id="delete"><i class="layui-icon"></i>删除</button>
          <button class="layui-btn layui-btn-normal" th:onclick="|xadmin.open('添加用户','@{/user/add}',800,600)|"><i
                  class="layui-icon"></i>添加
          </button>
          <button class="layui-btn layui-btn-warm" id="update">
            <i class="layui-icon">&#xe642;</i>编辑
          </button>
        </div>

        <div class="layui-card-body ">
          <table class="layui-hide" id="userTable"></table>
        </div>
      </div>
    </div>
  </div>
</div>
</body>



<script>
  var tableIns;

  layui.use(['table','form'], function () {
    var table = layui.table;
    var form = layui.form;
    var $ = layui.$;

    tableIns = table.render({
      elem: '#userTable',
      url: '/user/list',
      method: "post",
      cols: [
        [
          {type: 'checkbox', fixed: 'left'},
          {field: 'userId', width: 80, title: 'ID', sort: true},
          {field: 'username', width: 80, title: '用户名'},
          {field: 'password', title: '密码'},
          {field: 'role', title: '角色',templet(d){
                return `
                <a title=${d.role.roleName} onclick="xadmin.open('角色编辑','/user/power?roleId='+${d.role.roleId},800,600)" href="javascript:;" style="color: blue">
                                        <i class="layui-icon">${d.role.roleName}</i>
                                      </a>
                `;
            }
            },
          {field: 'userLockout', title: '状态', templet(d){
              if(d.userLockout==1){
                return '<p style="color: #3bdc12">有效用户</p>'
              }else{
                return '<p style="color: red">锁定</p>'
              }
            }
            },
          /*{
            field: 'action', title: '操作', templet(d) {
              // 反引号 ，在里面写字符串可以换行
              //          取变量， ${}
              // var editUrl = ctx + "/user/edit?id="+d.id;
              var editUrl = `${ctx}/user/edit?id=${d.id}`;

              return `
                                <div class="td-manage">
                                      <a onclick="member_stop(this,'10001')" href="javascript:;"  title="启用">
                                        <i class="layui-icon">&#xe601;</i>
                                      </a>
                                      <a title="编辑"  onclick="xadmin.open('编辑','${editUrl}',600,400)" href="javascript:;">
                                        <i class="layui-icon">&#xe642;</i>
                                      </a>
                                      <a onclick="xadmin.open('修改密码','member-password.html',600,400)" title="修改密码" href="javascript:;">
                                        <i class="layui-icon">&#xe631;</i>
                                      </a>
                                      <a title="删除" onclick="userDel(${d.id})" href="javascript:;">
                                        <i class="layui-icon">&#xe640;</i>
                                      </a>
                                </div>
                            `
            }
           },*/
        ]
      ],
      page: true,
      parseData(res) {
        return {
          "code": res.success ? 0 : 500,
          "msg": res.msg,
          "count": res.data.total,
          "data": res.data.list
        }
      }
    });

    //删除
    var dpath=ctx+'/user/delete';
    $('#delete').on('click',function (){
      var checkStatus = table.checkStatus('userTable'),data = checkStatus.data;
      if(data.length){
        layer.confirm('确认要删除吗？',function(index){
          var userIds="";
          for (var i=0;i<data.length;i++){
            if(i==data.length-1){
              userIds+=data[i].userId;
            }else{
              userIds+=data[i].userId+',';
            }
          }
          $.post(dpath,{"userIds":userIds},function (res){
            if(res.success){
              layer.msg(res.msg,{icon:1,time:1000},function(){
                location.reload();
              });
            }else {
              if(res.msg==null){
                layer.msg(res,{icon:2,time:2000});
              }else {
                layer.msg(res.msg,{icon:2,time:2000});
              }
            }
          });
        });
      }else {
        layer.msg('请选择要删除的数据',{icon:0,time:1000});
      }
    })

    //编辑
    var upath=ctx+'/user/update?userId=';
    $('#update').on('click',function (){
      var checkStatus = table.checkStatus('userTable'),data = checkStatus.data;
      if(data.length==1){
        xadmin.open('编辑用户',upath+data[0].userId,800,600);
      }else if (data.length==0){
        layer.msg('请选择要编辑的数据',{icon:0,time:1000});
      }else {
        layer.msg('只能选择一条数据编辑',{icon:0,time:1000});
      }
    })




//监听提交
    form.on('submit(search)',
            function (data) {
              tableIns.reload({
                where: data.field,
                page: {
                  curr: 1
                }
              })
              return false;
            });

  });



</script>



</html>
